package com.data.modules.health.service.impl;

import com.data.modules.health.entity.HlMainDetail;
import com.data.modules.health.entity.HlTestMain;
import com.data.modules.health.mapper.HlMainDetailMapper;
import com.data.modules.health.mapper.HlTestMainMapper;
import com.data.modules.health.service.IHlTestMainService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 测试主表
 * @Author: jeecg-boot
 * @Date:   2023-07-13
 * @Version: V1.0
 */
@Service
public class HlTestMainServiceImpl extends ServiceImpl<HlTestMainMapper, HlTestMain> implements IHlTestMainService {

	@Autowired
	private HlTestMainMapper hlTestMainMapper;
	@Autowired
	private HlMainDetailMapper hlMainDetailMapper;

	@Override
	@Transactional
	public void saveMain(HlTestMain hlTestMain, List<HlMainDetail> hlMainDetailList) {
		hlTestMainMapper.insert(hlTestMain);
		if(hlMainDetailList!=null && hlMainDetailList.size()>0) {
			for(HlMainDetail entity:hlMainDetailList) {
				//外键设置
				entity.setMainId(hlTestMain.getId());
				hlMainDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(HlTestMain hlTestMain,List<HlMainDetail> hlMainDetailList) {
		hlTestMainMapper.updateById(hlTestMain);

		//1.先删除子表数据
		hlMainDetailMapper.deleteByMainId(hlTestMain.getId());

		//2.子表数据重新插入
		if(hlMainDetailList!=null && hlMainDetailList.size()>0) {
			for(HlMainDetail entity:hlMainDetailList) {
				//外键设置
				entity.setMainId(hlTestMain.getId());
				hlMainDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		hlMainDetailMapper.deleteByMainId(id);
		hlTestMainMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			hlMainDetailMapper.deleteByMainId(id.toString());
			hlTestMainMapper.deleteById(id);
		}
	}

}
